#include "bsp_voice.h"
#include "driver/uart.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "bsp_param.h"


static void voice_volume_set(uint8_t volume)
{
    uint8_t buf[5]; 
    buf[0] = 0x7e;  
    buf[1] = 0x03;  
    buf[2] = 0x31;  
    buf[3] = volume;    
    buf[4] = 0xef;  
    uart_write_bytes(UART_NUM_2, buf, sizeof(buf));   
}

void voice_volume_shift(uint8_t volume_value)
{

    switch(volume_value){
        case MUSIC_CLOSE:
            voice_volume_set(0);
            break;
        case MUSIC_VOICE1:
            voice_volume_set(5);
            break;
        case MUSIC_VOICE2:
            voice_volume_set(10);
            break;
        case MUSIC_VOICE3:
            voice_volume_set(15);
            break;
        default:
            break;

    }
}
void voice_close_output()
{
    uint8_t buf[4]; 
    buf[0] = 0x7e;  
    buf[1] = 0x02;  
    buf[2] = 0x0e;   
    buf[3] = 0xEF;    
    uart_write_bytes(UART_NUM_2, buf, sizeof(buf));  

}


void voice_output(uint8_t _music_vol)
{
    uint8_t buf[6]; 
    // buf[0] = 0x7e;  
    // buf[1] = 0x04;  
    // buf[2] = 0x45;  
    // buf[3] = 0x00;    
    // buf[4] = 0x02;  
    // buf[5] = 0xEF;    
    // uart_write_bytes(UART_NUM_2, buf, sizeof(buf));   

    /* 循环播放 */
    buf[0] = 0x7e;  
    buf[1] = 0x03;  
    buf[2] = 0x07;  
    buf[3] = 0x01;  
    buf[4] = 0xef;    
    uart_write_bytes(UART_NUM_2, buf, sizeof(buf));    
    voice_volume_shift(_music_vol);
    /* 循环播放 */
    // buf[0] = 0x7e;  
    // buf[1] = 0x03;  
    // buf[2] = 0x33;  
    // buf[3] = 0x01;    
    // buf[4] = 0xef;  
    // uart_write_bytes(UART_NUM_2, buf, sizeof(buf));     
}

void voice_init()
{
 
    const uart_config_t uart_config = {
        .baud_rate = 115200,
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
        .source_clk = UART_SCLK_DEFAULT,
    };
        /* gpio中断 */
	gpio_config_t spk_gpio_config = {
		.intr_type = GPIO_INTR_DISABLE,
		.pin_bit_mask = 1ULL<<GPIO_SPEAKER_ENABLE,
		.mode = GPIO_MODE_OUTPUT,
		.pull_up_en = 0,
		.pull_down_en = 0,
	};
	gpio_config(&spk_gpio_config);//配置完默认是开启中断

    uart_param_config(UART_NUM_2, &uart_config);
    uart_set_pin(UART_NUM_2, VOICE_UART_TX_PORT, VOICE_UART_RX_PORT, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    uart_driver_install(UART_NUM_2, 1024, 0, 0, NULL, 0);

    gpio_reset_pin(GPIO_SPEAKER_ENABLE);
    gpio_set_direction(GPIO_SPEAKER_ENABLE,GPIO_MODE_OUTPUT);
    gpio_set_level(GPIO_SPEAKER_ENABLE, 1);
    voice_close_output();

}














